package com.company.cloud.acct.client;

import com.company.cloud.acct.modules.api.request.AcctGetRequest;
import com.company.cloud.acct.modules.api.request.AcctOpRequest;
import com.company.cloud.acct.modules.api.response.AccountModel;
import com.company.cloud.acct.modules.api.service.AccountRemoteService;
import com.company.cloud.acct.modules.app.service.AcctOpService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Service
public class AccountRemoteServiceImpl implements AccountRemoteService {

    @Autowired
    private AcctOpService acctOpService;

    @Override
    public BaseClientResult<String> opAcct(AcctOpRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo = new LoginSimpleUserInfo();
            acctOpService.acctOp(request, getDefUserInfo(request.getOpUserId(), request.getOpIp()));
            return BaseClientResult.success();
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<AccountModel> getAcct(AcctGetRequest request) {
        try {
            ValidatorUtils.validate(request);
            AccountModel data = acctOpService.getAcctInfo(request.getUserId(), request.getType());
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    private LoginSimpleUserInfo getDefUserInfo(Integer userId, String ip) {
        LoginSimpleUserInfo userInfo = new LoginSimpleUserInfo();
        userInfo.setId(userId);
        userInfo.setIp(ip);
        return userInfo;
    }
}
